#include<cstdio>
#include<acl/acl.h>

aclError check_ret(aclError ret, const char * msg) {
    if(msg==nullptr) {
        fprintf(stderr, "msg is NULLPTR\n");
    }
    if(ret==0) fprintf(stderr, "[SUCC] %s Successful\n", msg);
    else fprintf(stderr, "[ERR]  %s Failed, aclError = %d\n", msg, ret);
}

const char modelPath[] = "/home/HwHiAiUser/yolov3_tf_aipp.om";
uint32_t modelId;

int main() {
    printf("Program starts.\n");
    /* Initialize */
    aclError ret = aclInit(NULL);
    check_ret(ret, "Run aclInit()");

    /* Device */
    uint32_t count = -1;
    ret =  aclrtGetDeviceCount(&count);
    check_ret(ret, "Run aclrtGetDevice()");
    printf("there are %d device(s).\n", (int)count);
    int deviceId = -1;
    if(count > 0) {
        deviceId = 0;
        ret = aclrtSetDevice(deviceId);
        check_ret(ret, "Run aclrtSetDevice");
    }
    else {
        fprintf(stderr, "No Available Device.\n");
    }
    /* Context */
    aclrtContext context;
    ret = aclrtCreateContext(&context, deviceId);
    check_ret(ret, "aclrtCreateContext");

    /* Stream */
    aclrtStream stream;
    ret = aclrtCreateStream(&stream);

    /* Model loading */
    ret = aclmdlLoadFromFile(modelPath, &modelId);
    check_ret(ret, "LOAD MODEL");
    /* Inferencing */

    /* Finalize everything */
    ret = aclrtDestroyStream(stream); check_ret(ret, "aclrtDestroyStream");
    ret = aclrtDestroyContext(context); check_ret(ret, "aclrtDestroyContext");
    ret = aclrtResetDevice(deviceId); check_ret(ret, "aclrtResetDevice");
    ret = aclFinalize(); check_ret(ret, "aclFinalize");
    check_ret(ret, "Run aclFinalize()");
}